package com.lonewalker.sparrow.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yitter.idgen.YitIdHelper;
import com.lonewalker.sparrow.convertor.UserConvertor;
import com.lonewalker.sparrow.entity.SysUser;
import com.lonewalker.sparrow.mapper.UserMapper;
import com.lonewalker.sparrow.req.UserQueryReq;
import com.lonewalker.sparrow.req.UserReq;
import com.lonewalker.sparrow.service.UserService;
import com.lonewalker.sparrow.util.BizAssert;
import com.lonewalker.sparrow.util.IpUtil;
import com.lonewalker.sparrow.vo.SysUserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 用户信息表 服务层实现。
 *
 * @author lonewalker
 * @since 1.0
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    private final UserMapper userMapper;

    @Override
    public Boolean saveUser(UserReq requestParam) {
        log.info("保存用户数据接口相关入参:用户账号-{}",requestParam.getAccount());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        String ip = IpUtil.getIp(request);
        log.info("请求端ip地址："+ip);
        SysUser user = UserConvertor.INSTANCE.toEntity(requestParam);
        user.setId(YitIdHelper.nextId());
        int insertResult = baseMapper.insert(user);

        CompletableFuture.runAsync(() -> queryUser(insertResult, user.getId()));

        return true;
//        return this.save(user);
    }

    private void queryUser(int insertResult, Long id) {
        if (insertResult > 0){
            System.out.println("数据保存结果:"+insertResult);
            SysUser user = this.getById(id);
            log.info("查询用户数据接口相关入参:用户id-{}",user.getId());
        }
    }


    @Override
    public Boolean updateById(UserReq requestParam) {
        BizAssert.throwServiceError(ObjectUtil.isNull(requestParam.getId()), "用户id不能为空");
        SysUser user = this.getById(requestParam.getId());
        user.setAccount(requestParam.getAccount());
        user.setName(requestParam.getName());
        user.setNickName(requestParam.getNickName());
        user.setPassword(requestParam.getPassword());
        user.setLeader(requestParam.getLeader());
        user.setGender(requestParam.getGender());
        user.setOrgId(requestParam.getOrgId());
        return this.updateById(user);
    }

    @Override
    public List<SysUserVo> listUser() {
        return userMapper.listUser();
    }

    @Override
    public Page<SysUserVo> pageUser(UserQueryReq requestParam) {
        Page<SysUserVo> page = new Page<>(requestParam.getPageNum(), requestParam.getPageSize());
        return userMapper.pageUser(page, requestParam);
    }
}
